﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;

namespace NETCORE.TAttribute.Valide
{
    public class ModelValidateFilter : IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (!context.ModelState.IsValid)
            {
                var allErrors = context.ModelState.Values.SelectMany(v => v.Errors);
                var message = string.Join(" | ", allErrors.Select(e => e.ErrorMessage));
                context.Result = new JsonResult(new { results = allErrors });
            }
            await next();
        }
    }
}
